//
//  WebContentViewController.m
//  ASHT_IOS
//
//  Created by Hui Li on 14-4-21.
//  Copyright (c) 2014年 宋安安. All rights reserved.
//

#import "WebContentViewController.h"
#import "JSONKit.h"
#import "NSString+URLEncoding.h"
#import "RigthHomeViewController.h"
#import "MBProgressHUD.h"




@interface WebContentViewController ()

@end

@implementation WebContentViewController

@synthesize requestStr;
@synthesize titleStr;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _Adaptation=YES;
        size=100;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    newsWebView.delegate = self;
    RigthHomeViewController *rig=[[RigthHomeViewController alloc] init];
    btn2.selected=[rig.delegate DetermineWhetherIsAlready:self.key];
    [self initView];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(void) initView
{
    
    textFontSize = 50;
    label_title.text = titleStr;
    NSURL *url= [NSURL URLWithString:requestStr];
    NSString *a=@"%";
    NSString *str=[NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%i%@'",size,a];
    NSLog(@"%@",str);
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [newsWebView stringByEvaluatingJavaScriptFromString:str];
    [self webViewDidFinishLoad:newsWebView];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [newsWebView loadRequest:request];
    
 
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
    {
        if (_Adaptation) {
            self.view_Content.frame=CGRectMake(self.view_Content.frame.origin.x,self.view_Content.frame.origin.y,self.view_Content.frame.size.width,self.view_Content.frame.size.height-20);
            _Adaptation=NO;
        }
    }
}
-(void)btn1_Onclick:(id)sender
{
    
    if (size<110) {
        size=size+15;
    }
    else
    {
        size=100;
    }
    NSString *a=@"%";
    NSString *str=[NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%i%@'",size,a];
    NSLog(@"%@",str);
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [newsWebView stringByEvaluatingJavaScriptFromString:str];
    [self webViewDidFinishLoad:newsWebView];
    

}
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    NSString *message=[self ErrorMessageHandling:error];
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示"
                                                  message:message
                                                 delegate:nil
                                        cancelButtonTitle:@"确定"
                                        otherButtonTitles:nil];
    [alert show];
    alert=nil;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}


-(void)btn2_Onclick:(id)sender
{
    RigthHomeViewController *rig=[[RigthHomeViewController alloc] init];
    if (btn2.selected) {
       
        NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
        [dic setValue:self.requestStr forKey:self.key];
        
        if ([rig.delegate deleteFavorites:dic]) {
             btn2.selected=NO;
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示"
                                                         message:@"此文章从收藏夹移除"
                                                        delegate:self
                                               cancelButtonTitle:@"确定"
                                               otherButtonTitles:nil, nil];
            [alert show];
            alert=nil;
        }
    }
    else
    {
        //收藏成功
        
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        [dic setValue:requestStr forKey:self.key];
        if ([rig.delegate addFavorites:dic]) {
            btn2.selected=YES;
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示"
                                                         message:@"收藏成功"
                                                        delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            alert=nil;
        }
        
    }
    rig=nil;
}

-(void)btn3_Onclick:(id)sender
{

    id<ISSContent> publishContent = [ShareSDK content:requestStr
                                       defaultContent:@"默认分享内容，没内容时显示"
                                                image:nil
                                                title:self.titleStr
                                                  url:self.requestStr
                                          description:nil
                                            mediaType:SSPublishContentMediaTypeNews];
    //QQ空间
    [publishContent addQQSpaceUnitWithTitle:NSLocalizedString(self.requestStr, self.titleStr)
                                        url:INHERIT_VALUE
                                       site:nil
                                    fromUrl:nil
                                    comment:INHERIT_VALUE
                                    summary:INHERIT_VALUE
                                      image:INHERIT_VALUE
                                       type:INHERIT_VALUE
                                    playUrl:nil
                                       nswb:nil];
    //微信
    [publishContent addWeixinSessionUnitWithType:INHERIT_VALUE
                                         content:INHERIT_VALUE
                                           title:NSLocalizedString(self.requestStr, self.titleStr)
                                             url:INHERIT_VALUE
                                      thumbImage:[ShareSDK imageWithUrl:nil]
                                           image:INHERIT_VALUE
                                    musicFileUrl:nil
                                         extInfo:nil
                                        fileData:nil
                                    emoticonData:nil];
    [publishContent addSMSUnitWithContent:requestStr];
    
    
    id<ISSContainer> container = [ShareSDK container];
    [container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDirectionUp];
    
    id<ISSAuthOptions> authOptions = [ShareSDK authOptionsWithAutoAuth:YES
                                                         allowCallback:YES
                                                         authViewStyle:SSAuthViewStyleFullScreenPopup
                                                          viewDelegate:nil
                                               authManagerViewDelegate:nil];
    
    id<ISSShareActionSheetItem> sinaItem = [ShareSDK shareActionSheetItemWithTitle:[ShareSDK getClientNameWithType:ShareTypeSinaWeibo]
                                                                              icon:[ShareSDK getClientIconWithType:ShareTypeSinaWeibo]
                                                                      clickHandler:^{
                                                                          [ShareSDK shareContent:publishContent
                                                                                            type:ShareTypeSinaWeibo
                                                                                     authOptions:authOptions
                                                                                   statusBarTips:YES
                                                                                          result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                                                                              
                                                                                              if (state == SSPublishContentStateSuccess)
                                                                                              {
                                                                                                  NSLog(NSLocalizedString(@"TEXT_SHARE_SUC", @"分享成功"));
                                                                                              }
                                                                                              else if (state == SSPublishContentStateFail)
                                                                                              {
                                                                                                  NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"分享失败,错误码:%d,错误描述:%@"), [error errorCode], [error errorDescription]);
                                                                                              }
                                                            
                                                                                          }];
                                                                      }];
    
    id<ISSShareActionSheetItem> qzoneItem = [ShareSDK shareActionSheetItemWithTitle:[ShareSDK getClientNameWithType:ShareTypeQQSpace]
                                                                               icon:[ShareSDK getClientIconWithType:ShareTypeQQSpace]
                                                                       clickHandler:^{
                                                                           [ShareSDK shareContent:publishContent
                                                                                             type:ShareTypeQQSpace
                                                                                      authOptions:authOptions
                                                                                    statusBarTips:YES
                                                                                           result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                                                                               
                                                                                               if (state == SSPublishContentStateSuccess)
                                                                                               {
                                                                                                   NSLog(NSLocalizedString(@"TEXT_SHARE_SUC", @"分享成功"));
                                                                                               }
                                                                                               else if (state == SSPublishContentStateFail)
                                                                                               {
                                                                                                   NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"分享失败,错误码:%d,错误描述:%@"), [error errorCode], [error errorDescription]);
                                                                                               }
                                                                                           }];
                                                                       }];
    id<ISSShareActionSheetItem> wxsItem = [ShareSDK shareActionSheetItemWithTitle:[ShareSDK getClientNameWithType:ShareTypeWeixiSession]
                                                                             icon:[ShareSDK getClientIconWithType:ShareTypeWeixiSession]
                                                                     clickHandler:^{
                                                                         [ShareSDK clientShareContent:publishContent
                                                                                                 type:ShareTypeWeixiSession
                                                                                        statusBarTips:YES
                                                                                               result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                                                                                   
                                                                                                   if (state == SSPublishContentStateSuccess)
                                                                                                   {
                                                                                                       NSLog(NSLocalizedString(@"TEXt_SHARE_SUC", @"分享成功"));
                                                                                                   }
                                                                                                   else if (state == SSPublishContentStateFail)
                                                                                                   {
                                                                                                       NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"分享失败,错误码:%d,错误描述:%@"), [error errorCode], [error errorDescription]);
                                                                                                   }
                                                                                               }];
                                                                     }];
    
    id<ISSShareActionSheetItem> wxtItem = [ShareSDK shareActionSheetItemWithTitle:[ShareSDK getClientNameWithType:ShareTypeWeixiTimeline]
                                                                             icon:[ShareSDK getClientIconWithType:ShareTypeWeixiTimeline]
                                                                     clickHandler:^{
                                                                         [ShareSDK clientShareContent:publishContent
                                                                                                 type:ShareTypeWeixiTimeline
                                                                                        statusBarTips:YES
                                                                                               result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                                                                                   
                                                                                                   if (state == SSPublishContentStateSuccess)
                                                                                                   {
                                                                                                       NSLog(NSLocalizedString(@"TEXT_SHARE_SUC", @"分享成功"));
                                                                                                   }
                                                                                                   else if (state == SSPublishContentStateFail)
                                                                                                   {
                                                                                                       NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"分享失败,错误码:%d,错误描述:%@"), [error errorCode], [error errorDescription]);
                                                                                                   }
                                                                                               }];
                                                                     }];

    id<ISSShareActionSheetItem> qqItem = [ShareSDK shareActionSheetItemWithTitle:[ShareSDK getClientNameWithType:ShareTypeQQ]
                                                                            icon:[ShareSDK getClientIconWithType:ShareTypeQQ]
                                                                    clickHandler:^{
                                                                        [ShareSDK clientShareContent:publishContent
                                                                                                type:ShareTypeQQ
                                                                                       statusBarTips:YES
                                                                                              result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                                                                                  
                                                                                                  if (state == SSPublishContentStateSuccess)
                                                                                                  {
                                                                                                      NSLog(NSLocalizedString(@"TEXT_SHARE_SUC", @"分享成功"));
                                                                                                  }
                                                                                                  else if (state == SSPublishContentStateFail)
                                                                                                  {
                                                                                                      NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"分享失败,错误码:%d,错误描述:%@"), [error errorCode], [error errorDescription]);
                                                                                                  }
                                                                                              }];
                                                                    }];
    

    
    NSArray *shareList = [ShareSDK customShareListWithType:
                          sinaItem,
                          qzoneItem,
                          wxsItem,
                          wxtItem,
                          qqItem,
                          SHARE_TYPE_NUMBER(ShareTypeSMS),
                          nil];
    
    [ShareSDK showShareActionSheet:container
                         shareList:shareList
                           content:publishContent
                     statusBarTips:YES
                       authOptions:authOptions
                      shareOptions:[ShareSDK defaultShareOptionsWithTitle:nil
                                                          oneKeyShareList:[NSArray defaultOneKeyShareList]
                                                           qqButtonHidden:NO
                                                    wxSessionButtonHidden:NO
                                                   wxTimelineButtonHidden:NO
                                                     showKeyboardOnAppear:NO
                                                        shareViewDelegate:nil
                                                      friendsViewDelegate:nil
                                                    picViewerViewDelegate:nil]
                            result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                
                                if (state == SSPublishContentStateSuccess)
                                {
                                    NSLog(NSLocalizedString(@"TEXT_SHARE_SUC", @"发表成功"));
                                }
                                else if (state == SSPublishContentStateFail)
                                {
                                    NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"发布失败!error code == %d, error code == %@"), [error errorCode], [error errorDescription]);
                                }
                            }];


}

@end
